﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test_TabControl
{
    public class Test_TabControl: TabControl
    {

        public Test_TabControl()
        {
            //防止闪耀
            this.SetStyle(ControlStyles.DoubleBuffer |
                            ControlStyles.ResizeRedraw |
                            ControlStyles.AllPaintingInWmPaint |
                            ControlStyles.SupportsTransparentBackColor |
                            ControlStyles.OptimizedDoubleBuffer, true);

            this.DrawMode = TabDrawMode.OwnerDrawFixed;

            // tabcontrol设置为可以自定义绘制标签内容
            //this.DrawItem += new DrawItemEventHandler(this.event_tabControl_DrawItem);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            try
            {
                #region 改变工作区，工作区这里主要是背景色，因为我Form的背景色就是(45, 45, 48)，为了搭配起来好看
                //绑定工作区
                Rectangle rec01 = this.ClientRectangle;
                //设置工作区背景色
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(45, 45, 48)), rec01);
                #endregion

                #region 重绘标签头=======================
                //灰色背景
                SolidBrush back = new SolidBrush(Color.FromArgb(45, 45, 48));
                //蓝色字体
                SolidBrush white = new SolidBrush(Color.FromArgb(122, 193, 255));
                StringFormat sf = new StringFormat();
                //文本水平/垂直居中对齐
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                for (int i = 0; i < this.TabPages.Count; i++)
                {
                    //绑定选项卡
                    Rectangle rec = this.GetTabRect(i);
                    //设置选项卡背景
                    e.Graphics.FillRectangle(back, rec);

                    //设置选项卡字体及颜色
                    e.Graphics.DrawString(this.TabPages[i].Text, new Font("微软雅黑", 9), white, rec, sf);
                }
            #endregion


                //base.OnDrawItem(e);
                //Rectangle backrect = this.GetTabRect(e.Index);            
                //Font tabFont = SystemInformation.MenuFont;
                //SolidBrush fontbrush;
                //Brush backbrush;

                //#region  
                //Brush backtabcontrol = new SolidBrush(Color.FromArgb(45, 45, 48));
                //e.Graphics.FillRectangle(backtabcontrol, this.ClientRectangle);
                //backtabcontrol.Dispose();
                //#endregion

                //#region  设置选择的标签的背景
                //if (e.State == DrawItemState.Selected)  //if (e.State == DrawItemState.Selected)   if (e.Index == this.SelectedIndex)
                //{
                //    backbrush = new SolidBrush(Color.FromArgb(255, 43, 87, 255));
                //    fontbrush = new SolidBrush(Color.White);
                //}
                //else
                //{
                //    backbrush = new SolidBrush(Color.FromArgb(255, 43, 87, 154));
                //    fontbrush = new SolidBrush(Color.Black);
                //}

                ////绘制标签背景
                //e.Graphics.FillRectangle(backbrush, backrect);

                //#endregion


                //#region 重写标签名
                //StringFormat _StringFlags = new StringFormat(StringFormatFlags.DirectionRightToLeft);
                //_StringFlags.Alignment = StringAlignment.Center;
                //_StringFlags.LineAlignment = StringAlignment.Center;
                //e.Graphics.DrawString(this.TabPages[e.Index].Text, tabFont, fontbrush, e.Bounds, _StringFlags);
                //#endregion

                //backbrush.Dispose();
                //tabFont.Dispose();
                //fontbrush.Dispose();

            }
            catch (Exception er)
            {
                ;
            }
        }

    }
}
